/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-04-13
 * Time: 18:04
 */

public class MyCircularQueue {
    public int[] elem;
    public int front;
    public int rear;

    public MyCircularQueue(int key){
        this.elem = new int[key+1];
    }

    // 入队
    public void offer(int val){
        if (isFull()){
            System.out.println("循环队列已满");
            return;
        }
        this.elem[rear] = val;
        rear = (rear+1) % elem.length;
    }

    public void poll(){
        if (isEmpty()){
            System.out.println("循环队列为空");
        }
        front = (front+1)%elem.length;

    }

    public void peek(){ //得到队首元素
        if (isEmpty()){
            System.out.println("循环队列为空");
        }
        System.out.println(elem[front]);
    }

    public void tail(){ // 得到队尾元素
        if (isEmpty()){
            System.out.println("循环队列为空！");
        }
        int index = 0;
        if (rear == 0){
            index = elem.length-1;
        }else {
            index = rear - 1;
        }
        System.out.println(elem[index]);
    }


    public boolean isEmpty(){
        return front == rear;
    }

    public boolean isFull(){
        if ((this.rear+1) % elem.length == front){
            return true;
        }
        return false;
    }


    public void display(){
        for (int i = 0; i < elem.length; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }

}
